//
//  UIButton+Category.m
//  XKOneHun
//
//  Created by xyg on 2016/12/9.
//  Copyright © 2016年 xiaokang100. All rights reserved.
//

#import "UIButton+Category.h"
#import <objc/runtime.h>



@implementation UIButton (Category)

static NSString *keyOfUseCategoryMethod;//用分类方法创建的button，关联对象的key
static NSString *keyOfBlock;


+ (UIButton *)getClickBtnWithTitle:(NSString *)title{
    UIButton *btn = [self buttonWithType:UIButtonTypeCustom];
    btn.enabled = NO;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:WhiteColor forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageWithColor:kBaseRedColor] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageWithColor:LightGrayColor] forState:UIControlStateDisabled];
    [btn setBackgroundImage:[UIImage imageWithColor:LightGrayColor] forState:UIControlStateHighlighted];
    [btn setTitleColor:WhiteColor forState:UIControlStateNormal];
    [btn setTitleColor:GrayColor forState:UIControlStateDisabled];
    [btn setTitleColor:GrayColor forState:UIControlStateSelected];
    btn.titleLabel.font = [UIFont systemFontOfSize:15*Scale_W];
    btn.layer.cornerRadius = 5.0;
    btn.layer.masksToBounds = YES;
    return btn;
}



+ (UIButton *)getBtnWithFrame:(CGRect)frame
                        title:(NSString *)title
                         font:(CGFloat)font
                  normalColor:(UIColor *)normalColor
               highlightColor:(UIColor *)highlightColor
                  actionBlock:(tapActionBlock)actionBlock{
    return [self getBtnWithFrame:frame
                      nomalTitle:title
                  highlightTitle:title
                       titleFont:font
                     normalColor:normalColor
                  highlightColor:highlightColor
                      nomalImage:nil
                  highlightImage:nil
                 backgroundColor:nil
           normalBackgroungImage:nil
        highlightBackgroungImage:nil
                     actionBlock:actionBlock];
}



+ (UIButton *)getBtnWithTitle:(NSString *)title
                    titleFont:(CGFloat)font
                   titleColor:(UIColor *)titleColor
                  actionBlock:(tapActionBlock)actionBlock {
    return [self getBtnWithFrame:CGRectZero nomalTitle:title highlightTitle:nil titleFont:font normalColor:titleColor highlightColor:titleColor nomalImage:nil highlightImage:nil backgroundColor:nil normalBackgroungImage:nil highlightBackgroungImage:nil actionBlock:actionBlock];
}



+ (UIButton *)getBtnWithFrame:(CGRect)frame
                        title:(NSString *)title
                    titleFont:(CGFloat)font
                   titleColor:(UIColor *)titleColor
                  actionBlock:(tapActionBlock)actionBlock {
    return [self getBtnWithFrame:frame nomalTitle:title highlightTitle:nil titleFont:font normalColor:titleColor highlightColor:nil nomalImage:nil highlightImage:nil backgroundColor:nil normalBackgroungImage:nil highlightBackgroungImage:nil actionBlock:actionBlock];
}


+ (UIButton *)getBtnWithFrame:(CGRect)frame
                        title:(NSString *)title
                    titleFont:(CGFloat)font
                   titleColor:(UIColor *)titleColor
              backgroundColor:(UIColor *)backgroundColor
                  actionBlock:(tapActionBlock)actionBlock {
    return [self getBtnWithFrame:frame nomalTitle:title highlightTitle:nil titleFont:font normalColor:titleColor highlightColor:nil nomalImage:nil highlightImage:nil backgroundColor:backgroundColor normalBackgroungImage:nil highlightBackgroungImage:nil actionBlock:actionBlock];
}

+ (UIButton *)getBtnWithFrame:(CGRect)frame
                   nomalTitle:(NSString *)nomalTitle
               highlightTitle:(NSString *)highlightTitle
                    titleFont:(CGFloat)font
                  normalColor:(UIColor *)normalColor
               highlightColor:(UIColor *)highlightColor
              backgroundColor:(UIColor *)backgroundColor
                  actionBlock:(tapActionBlock)actionBlock {
    return [self getBtnWithFrame:frame nomalTitle:nomalTitle highlightTitle:highlightTitle titleFont:font normalColor:normalColor highlightColor:highlightColor nomalImage:nil highlightImage:nil backgroundColor:backgroundColor normalBackgroungImage:nil highlightBackgroungImage:nil actionBlock:actionBlock];
}


+ (UIButton *)getBtnWithFrame:(CGRect)frame
                   nomalTitle:(NSString *)nomalTitle
               highlightTitle:(NSString *)highlightTitle
                    titleFont:(CGFloat)font
                  normalColor:(UIColor *)normalColor
               highlightColor:(UIColor *)highlightColor
                   nomalImage:(NSString *)normalImage
               highlightImage:(NSString *)highlightImage
              backgroundColor:(UIColor *)backgroundColor
                  actionBlock:(tapActionBlock)actionBlock {
    return [self getBtnWithFrame:frame nomalTitle:nomalTitle highlightTitle:highlightTitle titleFont:font normalColor:normalColor highlightColor:highlightColor nomalImage:normalImage highlightImage:highlightImage backgroundColor:backgroundColor normalBackgroungImage:nil highlightBackgroungImage:nil actionBlock:actionBlock];
}



+ (UIButton *)getBtnWithFrame:(CGRect)frame
                   nomalTitle:(NSString *)nomalTitle
               highlightTitle:(NSString *)highlightTitle
                    titleFont:(CGFloat)font
                  normalColor:(UIColor *)normalColor
               highlightColor:(UIColor *)highlightColor
                   nomalImage:(NSString *)normalImage
               highlightImage:(NSString *)highlightImage
              backgroundColor:(UIColor *)backgroundColor
        normalBackgroungImage:(NSString *)normalBackgroungImage
     highlightBackgroungImage:(NSString *)highlightBackgroungImage
                  actionBlock:(tapActionBlock)actionBlock {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    button.titleLabel.font = [UIFont systemFontOfSize:font];
    [button setTitle:nomalTitle forState:UIControlStateNormal];
    [button setTitle:highlightTitle forState:UIControlStateHighlighted];
    [button setTitleColor:normalColor forState:UIControlStateNormal];
    [button setTitleColor:highlightColor forState:UIControlStateHighlighted];
    [button setBackgroundColor:backgroundColor];
    [button setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:highlightImage] forState:UIControlStateHighlighted];
    [button setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [button setBackgroundImage:[UIImage imageNamed:normalBackgroungImage] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:highlightBackgroungImage] forState:UIControlStateHighlighted];
    [button addTarget:button action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    
    /**
     *用runtime中的函数通过key关联对象
     *
     *objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
     *id object                     表示关联者，是一个对象，变量名理所当然也是object
     *const void *key               获取被关联者的索引key
     *id value                      被关联者，这里是一个block
     *objc_AssociationPolicy policy 关联时采用的协议，有assign，retain，copy等协议，一般使用OBJC_ASSOCIATION_RETAIN_NONATOMIC
     
     */
    objc_setAssociatedObject (button , &keyOfUseCategoryMethod , actionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC );
    
    return button;
}



- (void)tapAction:(UIButton*)sender
{
    /**
     * 通过key获取被关联对象
     *objc_getAssociatedObject(id object, const void *key)
     *
     */
    tapActionBlock block = ( tapActionBlock )objc_getAssociatedObject (sender , &keyOfUseCategoryMethod );
    
    if (block) {
        
        block(sender);
        
    }
}


- (void)setActionBlock:(tapActionBlock)actionBlock
{
    objc_setAssociatedObject (self , &keyOfBlock , actionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC );
    
}

- (tapActionBlock)actionBlock
{
    return objc_getAssociatedObject (self , &keyOfBlock );
}
@end
